import type { Profile } from '@/types/user';
import { removeLocalToken, setLocalToken } from './../utils/auth';
import { getLocalToken } from '@/utils/auth'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('hm-user', () => {
  // 优先从本地获取
  const token = ref(getLocalToken() || '')
  // 个人信息
  const profile = ref<Profile>({} as Profile)
  const setProfile = (p: Profile) => {
    profile.value = p
  }

  const setToken = (t: string) => {
    token.value = t
    // 本地存一份
    setLocalToken(t)
  }

  const clearUserInfo = () => {
    // 清除Token
    token.value = ''
    removeLocalToken()
  }
  return {token, setToken, clearUserInfo, profile, setProfile}
})
